package com.ruoyi.web.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.entity.Station;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import com.ruoyi.web.entity.Charges;
import com.ruoyi.web.service.IChargesService;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/admin/charges")
public class ChargesController extends BaseController {

    private final IChargesService chargesService;
    public ChargesController(IChargesService chargesServiceImpl){
        this.chargesService = chargesServiceImpl;
    }
    /**
     * 查询收费标准管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:charges:list')")
    @GetMapping("/list")
    public TableDataInfo list(Charges charges)
    {
        startPage();
        List<Charges> list = chargesService.selectChargesList(charges);
        return getDataTable(list);
    }

    @GetMapping("/getDatas")
    public AjaxResult getDatas() {
        List<Charges> list = chargesService.list();
        List<Map<String, Object>> datas = new ArrayList<>();
        for (Charges charges : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("stationId", charges.getId().toString());
            map.put("stationName", charges.getTypeName());
            datas.add(map);
        }
        return success(datas);
    }

    /**
     * 导出收费标准管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:charges:export')")
    @Log(title = "收费标准管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Charges charges) {
        List<Charges> list = chargesService.selectChargesList(charges);
        ExcelUtil<Charges> util = new ExcelUtil<Charges>(Charges.class);
        util.exportExcel(response, list, "收费标准管理数据");
    }

    /**
     * 获取收费标准管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:charges:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(chargesService.selectChargesById(id));
    }

    /**
     * 新增收费标准管理
     */
    @PreAuthorize("@ss.hasPermi('admin:charges:add')")
    @Log(title = "收费标准管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Charges charges) {
        charges.setId(IdUtil.getSnowflakeNextId());
        LoginUser loginUser = getLoginUser();
        String username = loginUser.getUsername();
        charges.setCreator(username);
        return toAjax(chargesService.insertCharges(charges));
    }

    /**
     * 修改收费标准管理
     */
    @PreAuthorize("@ss.hasPermi('admin:charges:edit')")
    @Log(title = "收费标准管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Charges charges) {
        LoginUser loginUser = getLoginUser();
        String username = loginUser.getUsername();
        charges.setLastModifier(username);
        charges.setLastModifiedAt(new Date());
        return toAjax(chargesService.updateCharges(charges));
    }

    /**
     * 删除收费标准管理
     */
    @PreAuthorize("@ss.hasPermi('admin:charges:remove')")
    @Log(title = "收费标准管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(chargesService.deleteChargesByIds(ids));
    }
}
